/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vistas;

import entidades.Deposito;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import rn.RepuestoRN;
import rn.VehiculoRN;
import utils.JTextFieldToUpperCase;

/**
 *
 * @author Pablo
 */
public class NuevaDeposito extends javax.swing.JDialog {

    /**
     * Creates new form NuevoDeposito
     */
    Deposito pr;
    
    public NuevaDeposito(java.awt.Frame parent, String titulo, Deposito pr) {
        super(parent, titulo);
        initComponents();
        this.setLocationRelativeTo(null);
        tf_descripcion.setDocument(new JTextFieldToUpperCase());
        tf_ubicacion.setDocument(new JTextFieldToUpperCase());
        tf_responsable.setDocument(new JTextFieldToUpperCase());
        
        tf_telefono.addKeyListener(new KeyAdapter() {            
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    bt_aceptar.requestFocus();
                }
            }
        });
        this.pr = pr;
        if(this.pr !=null){
            tf_descripcion.setText(pr.getDescripcion());
            tf_ubicacion.setText(pr.getUbicacion());
            tf_responsable.setText(pr.getResponsable());
            tf_telefono.setText(pr.getTelefono());
        }
        bt_aceptar.addKeyListener(new GrabarRegistro()); 
    }
    
    class GrabarRegistro extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getSource() == bt_aceptar) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    grabar();
                }
            }
        }
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_descripcion = new javax.swing.JTextField();
        bt_aceptar = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        tf_ubicacion = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_responsable = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tf_telefono = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Descripción:");

        tf_descripcion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        bt_aceptar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_aceptar.setText("Aceptar");
        bt_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_aceptarActionPerformed(evt);
            }
        });

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("Cancelar");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Ubicación:");

        tf_ubicacion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Responsable:");

        tf_responsable.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Teléfono:");

        tf_telefono.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_descripcion))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 166, Short.MAX_VALUE)
                        .addComponent(bt_aceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bt_cancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_ubicacion))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_responsable))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_telefono)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tf_ubicacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tf_responsable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tf_telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_aceptarActionPerformed
        grabar();
      
    }//GEN-LAST:event_bt_aceptarActionPerformed

    private void grabar(){
        Deposito p = new Deposito();
        if (verificarCampo(tf_descripcion)) {
            if (this.pr == null) {
                p.setDescripcion(tf_descripcion.getText());
                p.setUbicacion(tf_ubicacion.getText());
                p.setResponsable(tf_responsable.getText());
                p.setTelefono(tf_telefono.getText());
                if(!RepuestoRN.yaExisteDeposito(tf_descripcion.getText(), null)){
                    RepuestoRN.create(p);
                    this.dispose();
                }                    
                else
                    JOptionPane.showMessageDialog(null, "Deposito ya existe!!!"); 
                
            }
            else{
                pr.setDescripcion(tf_descripcion.getText());
                pr.setUbicacion(tf_ubicacion.getText());
                pr.setResponsable(tf_responsable.getText());
                pr.setTelefono(tf_telefono.getText());
                if(!RepuestoRN.yaExisteDeposito(tf_descripcion.getText(), pr.getId())){
                    RepuestoRN.edit(pr);
                    this.dispose();  
                }
                else
                    JOptionPane.showMessageDialog(null, "Deposito ya existe!!!");                  
            }
           ABM_Deposito.actualizarTabla(); 
        }        
        else{
            JOptionPane.showMessageDialog(null, "Descripcion no puede ser vacia");
        }
    }
    
    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    
    private boolean verificarCampo(JTextField f){
        if(f.getText().isEmpty() || f.getText().startsWith(" ")){
            return false;
        }
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_aceptar;
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_responsable;
    private javax.swing.JTextField tf_telefono;
    private javax.swing.JTextField tf_ubicacion;
    // End of variables declaration//GEN-END:variables
}
